package com.atguigu.day26.dml02;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class QueryTest {

    @Test
    public void test02() throws Exception{
        //1.注册驱动
        //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "root");
        //3.准备sql
        String sql  ="select * from t_account";
        //4.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //5.执行命令获取结果集
        ResultSet rs = pst.executeQuery();

        //6.处理结果集

        while (rs.next()){
            int aid = rs.getInt(1);
            String aname = rs.getString("aname");
            int balance = rs.getInt(3);

            System.out.println(aid+","+aname+","+balance);

        }
        //8.关闭资源
        rs.close();
        pst.close();
        connection.close();

    }


    @Test
    public void test01() throws Exception{
        //1.注册驱动
        //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "root");
        //3.准备sql
        String sql  ="select * from t_account";
        //4.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //5.执行命令获取结果集
        ResultSet rs = pst.executeQuery();
        //6.处理结果集
        while (rs.next()){
            Object aid = rs.getObject("aid");
            Object aname = rs.getObject(2);
            Object balance = rs.getObject("balance");
            //7.展示数据
            System.out.println("aid = " + aid+", aname = " + aname+", balance = " + balance);

        }
        //8.关闭资源
        rs.close();
        pst.close();
        connection.close();




    }
}
